package flst.ipiweb.struts.action.codepromo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.service.ServiceCodePromo;
import flst.ipiweb.transverse.CodePromotionnel;
import flst.ipiweb.transverse.IPIException;

public class DeleteCodePromoAction extends Action{

	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {
		
			//On récupère la session
			HttpSession session = request.getSession();
			if(session.getAttribute("login") == null)
			{
				return mapping.findForward("login");
			}
			
			CodePromotionnel codePromo = new CodePromotionnel();
			int idCodePromo = Integer.parseInt(request.getParameter("id"));
			codePromo.setIdCode(idCodePromo);

			ServiceCodePromo sCodePromo = new ServiceCodePromo();
			try
			{
				sCodePromo.deleteCodePromo(codePromo);
				//Message succès
				session.setAttribute("success","CodePromoSuccess.delete");
				session.setAttribute("error", null);
			}
			catch(IPIException e)
			{
				//Message d'erreur
				session.setAttribute("success",null);
				session.setAttribute("error", e.getMessage());
			}
	
			return mapping.findForward("list_code_promo");
		}
}
